<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="所属楼宇" prop="buildId">-->
<!--        <el-select v-model="queryParams.buildId" placeholder="请选择物业" size="mini">-->
<!--          <el-option v-for="build in buildOptions"-->
<!--                     :key="build.id"-->
<!--                     :label="build.buildName"-->
<!--                     :value="build.id"/>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="关键字" prop="realName">
        <el-input v-model="queryParams.realName" placeholder="请输入业主姓名或手机号或户号" clearable size="mini" style="width: 240px;"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane label="2024" name="0"/>
      <el-tab-pane label="2025" name="1"/>
    </el-tabs>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link :to="'/property/estate'" class="link-type">
          <el-button icon="el-icon-back" size="mini">
            返 回
          </el-button>
        </router-link>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['module:estateRoom:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['module:estateRoom:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon" type="warning" size="small" @click.stop="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

<!--    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">-->
<!--      <el-tab-pane v-for="type in yearList"-->
<!--                   :label="type.yearNum"-->
<!--                   :name="type.yearNum + ''" >-->
<!--      </el-tab-pane>-->
<!--    </el-tabs>-->

    <el-table v-loading="loading" :data="roomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="住宅ID" align="center" prop="id" />
      <el-table-column label="小区名称" align="center" prop="estateName" />
      <el-table-column label="户号" align="center" prop="roomNo" />
<!--      <el-table-column label="房间号" align="center" prop="roomNum" />-->
<!--      <el-table-column label="房屋面积" align="center" prop="roomArea">-->
<!--        <template slot-scope="scope">-->
<!--          <div>{{(scope.row.roomArea!=null ? scope.row.roomArea :'') +'㎡'}}</div>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="房屋类型" align="center" prop="roomType">-->
<!--        &lt;!&ndash;        0 - 高层 1 - 多层 2- 商业 3 - 别墅 4 - 其他&ndash;&gt;-->
<!--        <template slot-scope="scope">-->
<!--          <div>-->
<!--            <el-tag v-if="scope.row.roomType === 0" style="cursor: pointer" :type="''">高层</el-tag>-->
<!--            <el-tag v-if="scope.row.roomType === 1" style="cursor: pointer" :type="''">多层</el-tag>-->
<!--            <el-tag v-if="scope.row.roomType === 2" style="cursor: pointer" :type="''">商业</el-tag>-->
<!--            <el-tag v-if="scope.row.roomType === 3" style="cursor: pointer" :type="''">别墅</el-tag>-->
<!--            <el-tag v-if="scope.row.roomType === 4" style="cursor: pointer" :type="''">其他</el-tag>-->
<!--          </div>-->
<!--        </template>-->
<!--      </el-table-column>-->

      <el-table-column label="缴费至" align="center" prop="jiaofei" />
      <el-table-column label="房主姓名" align="center" prop="realName" />
      <el-table-column label="房主手机号" align="center" prop="phoneNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="350">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-user-solid">
            <router-link :to="'/property/fee/template?estateId=' + scope.row.estateId + '&roomId=' + scope.row.id+'&from=room&state=0'" class="link-type">
              物业费欠费明细
            </router-link>
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['module:estateRoom:edit']">修改</el-button>
<!--          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"-->
<!--                     v-hasPermi="['module:estateRoom:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

<!--    <pagination v-show="total > 0"-->
<!--                :total="total"-->
<!--                :page.sync="queryParams.pageNum"-->
<!--                :limit.sync="queryParams.pageSize"-->
<!--                @pagination="getList" />-->

    <!-- 添加或修改小区内的房屋列信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="所在小区" prop="estateId">
              <el-select v-model="form.estateId" placeholder="请选择小区" size="mini" style="width:100%;" filterable clearable>
                <el-option v-for="estate in estatesOptions"
                           :key="estate.id"
                           :label="estate.estateName"
                           :value="estate.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="房间号" prop="roomNo">
              <el-input v-model="form.roomNo" placeholder="请输入房间号" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="房屋面积" prop="roomArea">
              <el-input v-model="form.roomArea" placeholder="请输入房屋面积" size="mini">
                <template slot="append">㎡</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="房主姓名" prop="realName">
              <el-input v-model="form.realName" placeholder="请输入房主姓名" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="房主手机号" prop="phoneNum">
              <el-input v-model="form.phoneNum" placeholder="请输入房主手机号" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态" prop="state">
              <el-radio-group v-model="form.state" size="mini">
                <el-radio :value="1" :label="1">正常</el-radio>
                <el-radio :value="0" :label="0">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listRoom, getRoom, delRoom, addRoom, updateRoom, exportArrearage, arrearageList} from "@/api/module/estate/room";
  import { getToken } from "@/utils/auth";
  import { listAllEstate } from "@/api/module/estate/estate";
  import {listYearList} from "@/api/module/estate/wyfTemplate";
import {buildListAll, listBuild, listBuilds} from "@/api/module/estate/build";
  export default {
    name: "Room",
    data() {
      return {
        activeName:"0",
        // 按钮loading
        buttonLoading: false,
        qianfei:undefined,
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 小区内的房屋列信息表格数据
        roomList: [],
        estatesOptions:[],
        yearList:[],
        buildOptions:[],
        // 弹出层标题
        querybuild:{
          estateId:undefined,
        },
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          estateId: undefined,
          realName: undefined,
          roomNo:undefined,
          state:undefined,
          year:undefined,
          buildId:undefined,
        },
        queryYear:{
          estateId: undefined,
          state:undefined,
        },
        fromType:'',
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
      };
    },
    created() {
      this.queryParams.estateId = this.$route.query.estateId ? parseInt(this.$route.query.estateId) : undefined;
      this.querybuild.estateId = this.$route.query.estateId ? parseInt(this.$route.query.estateId) : undefined;
      this.queryYear.estateId = this.$route.query.estateId ? parseInt(this.$route.query.estateId) : undefined;
      this.fromType = this.$route.query.from ? this.$route.query.from : undefined;
      this.qianfei = this.$route.query.qianfei ? this.$route.query.qianfei : undefined;
      this.buildList();
      this.getYearList();
      this.getList();

    },
    methods: {
      /** 查询小区内的房屋列信息列表 */
      getList() {
        this.loading = true;
        if (this.activeName === "0"){
          this.queryParams.year=2024;
        }else if (this.activeName === "1"){
          this.queryParams.year=2025;
        }
        arrearageList(this.queryParams).then(response => {
          console.log(response)
          this.roomList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getYearList(){
        listYearList(this.queryYear).then(response =>{
          this.yearList=response.rows;
          console.log(response)
          console.log(this.yearList)
        })
      },
      getEstateList() {
        listAllEstate().then(response => {
          this.estatesOptions = response.data;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          estateId: this.queryParams.estateId,
          estateName: undefined,
          roomNo: undefined,
          buildNo: undefined,
          unitNo: undefined,
          roomNum: undefined,
          roomArea: undefined,
          roomType: undefined,
          realName: undefined,
          phoneNum: undefined,
          bangWxUserId: undefined,
          state: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length!==1;
        this.multiple = !selection.length
      },
      handleClick(val){
        console.log('handleClick',val.name);
        this.activeName = val.name;
        console.log(this.queryParams);
        this.getList();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.getEstateList();
        this.open = true;
        this.title = "添加小区内的房屋列信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        this.getEstateList();
        const id = row.id || this.ids;
        getRoom(id).then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = "修改小区内的房屋列信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            if (this.form.id != null) {
              updateRoom(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addRoom(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除小区内的房屋列信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delRoom(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出小区内的房屋欠费信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportArrearage(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      },
      buildList(){
        buildListAll(this.querybuild).then(response =>{
          this.buildOptions = response;
        })
      },
      /** 导入按钮操作 */
      handleImportWyf() {
        this.upload.title = "物业费数据导入";
        this.upload.open = true;
      },
      handleImportWyfYear() {
        this.uploadWyfYear.title = "物业费数据导入(按年)";
        this.uploadWyfYear.open = true;
      },
      /** 导入按钮操作 */
      handleImportSdJ() {
        this.uploadSdJ.title = "水电加压数据导入";
        this.uploadSdJ.open = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
        this.getList();
      },
      // 文件上传成功处理
      handleFileWyfYearSuccess(response, file, fileList) {
        this.uploadWyfYear.open = false;
        this.uploadWyfYear.isUploading = false;
        this.$refs.uploadWyfYear.clearFiles();
        this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
        this.getList();
      },


      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit();
      },
      submitFileFormWyfYear() {
        this.$refs.uploadWyfYear.submit();
      },

      // 文件上传中处理
      handleFileUploadProgressSdJ(event, file, fileList) {
        this.uploadSdJ.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccessSdJ(response, file, fileList) {
        this.uploadSdJ.open = false;
        this.uploadSdJ.isUploading = false;
        this.$refs.uploadSdJ.clearFiles();
        this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
        this.getList();
      },
      // 提交上传文件
      submitFileFormSdJ() {
        this.$refs.uploadSdJ.submit();
      }
    }
  };
</script>
